<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Services;

use Illuminate\Http\Request;

class BrokenService
{

    use Service;

    /**
     * 文件碎片展示
     *
     * @param Request $request
     *
     * @return mixed
     */
    public function BrokenIndexService( $request )
    {
        $error = $this->getErrorCode();
        if ( ! $request->filled('name') ) request()->offsetSet('name', 'file');
        if ( ! $request->filled('static') ) request()->offsetSet('static', 'public');
        $name          = $request->input('name', 'file');
        $static        = $request->input('static', 'public');
        $res[ 'name' ] = explode('/', $name);
        if ( $static != 'public' ) return back()->with('error', $error::message(1015));
        else if ( reset($res[ 'name' ]) != 'file' ) return back()->with('error', $error::message(1015));
        $disk                     = $this->getStorage($static);
        $pic                      = $disk->allFiles($name);//获取文件夹下所有文件
        $res[ 'name' ][ 'count' ] = count($pic);//获取总数
        $res[ 'pic' ]             = [];
        foreach ( $pic as $key => $value ) {//所有文件循环
            $name    = explode('/', $value);
            $picName = array_slice($name, 0, count($name) - 1);//根据条件取出文件夹名
            foreach ( $picName as $k => $v ) $k == 0 ? $res[ 'pic' ][ $key ][ 'name' ] = $v . '/' : $res[ 'pic' ][ $key ][ 'name' ] .= $v . '/';//循环拼接文件夹
            $res[ 'pic' ][ $key ][ 'title' ] = end($name);//去数组最后一个
            $res[ 'pic' ][ $key ][ 'last' ]  = $disk->time($value);//文件最后修改时间
            $res[ 'pic' ][ $key ][ 'size' ]  = $disk->size($value);//文件大小
        }
        return view('system.broken.index', compact('res', 'request'));
    }

    /**
     * 文件树
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function BrokenTreeService( $request )
    {
        $error  = $this->getErrorCode();
        $name   = $request->input('name', 'file');
        $static = $request->input('static', 'public');
        $names  = explode('/', $name);
        if ( $static != 'public' ) return message($error::message(1015));
        else if ( reset($names) != 'file' ) return message($error::message(1015));
        $public = $this->getCommon()->TreeChildService(reset($names), $static, $name, $request->input('true', ''));//树状态
        return data($public);
    }

    /**
     * 删除碎片
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
     */
    public function BrokenDeleteService( $request )
    {
        $error = $this->getErrorCode();
        $disk  = $this->getStorage($request->input('static'));
        $title = $request->input('title');
        $name  = $request->input('name');
        switch ( $request->input('status') ) {
            case 1:
                $str      = trimAll($title);//去除空格
                $name_arr = explode('/', $title);
                if ( reset($name_arr) != 'file' ) return back()->with('error', $error::message(1015));
                if ( ! $disk->exists($str) ) return back()->with('error', $error::message(1016));
                $disk->delete($str);//删除文件
                errorStorage('broken', '"删除文件名称" : ' . $title, '缓存文件删除成功', 'success', '删除缓存文件');
                return back()->with('success', $error::message(204));
                break;
            case 2:
                foreach ( $title as $value ) {//循环删除
                    $name_arr = explode('/', $value);
                    if ( reset($name_arr) != 'file' ) return message($error::message(1015));
                    $str = trimAll($value);//去除空格
                    if ( ! $disk->exists($str) ) return message($error::message(1016));
                    $disk->delete($str);//删除文件
                    errorStorage('broken', '"删除名称" : ' . $str, '缓存文件删除成功', 'success', '删除缓存文件');
                }
                return data(NULL, $error::message(204));
                break;
            case 3:
                $name_arr = explode('/', $name);
                if ( reset($name_arr) != 'file' ) return message($error::message(1015));
                $disk->deleteDirectory($name);//删除文件夹及文件夹下得所有文件
                errorStorage('broken', '"删除文件夹名称" : ' . $name, '文件夹删除成功', 'success', '删除文件夹');
                return data(NULL, $error::message(204));
            default:
                return back()->with('error', $error::message(1001));
                break;
        }
    }

}
